package com.cp.weixin.blog.dao;

import com.cp.weixin.blog.repository.Record;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Mapper
@Component("recordMapper")
public interface RecordMapper {

	@Insert("INSERT INTO record (uid, title, content, source, pubtime, publish_time, md5, query, read_count, state, category_id, tags) VALUES (" +
			"#{uid, jdbcType=VARCHAR}, " +
			"#{title, jdbcType=VARCHAR}, " +
			"#{content, jdbcType=VARCHAR}, " +
			"#{source, jdbcType=VARCHAR}, " +
			"#{pubtime, jdbcType=DATE}, " +
			"#{publish_time, jdbcType=DATE}, " +
			"#{md5, jdbcType=VARCHAR}, " +
			"#{query, jdbcType=VARCHAR}, " +
			"#{read_count, jdbcType=INTEGER}, " +
			"#{state, jdbcType=INTEGER}, " +
			"#{category_id, jdbcType=INTEGER}, " +
			"#{tags, jdbcType=VARCHAR} " +
			")")
	int addRecord(@Param("uid") String uid,
	              @Param("title") String title,
	              @Param("content") String content,
	              @Param("source") String source,
	              @Param("pubtime") Date pubtime,
	              @Param("publish_time") Date publish_time,
	              @Param("md5") String md5,
	              @Param("query") String query,
	              @Param("read_count") Integer read_count,
	              @Param("state") Integer state,
	              @Param("category_id") Integer category_id,
	              @Param("tags") String tags
	              );

	@Select("SELECT * FROM record WHERE uid = #{uid}")
	@Results(id = "allColumns", value = {
			@Result(id = true, column = "id", property = "id"),
			@Result(column = "title", property = "title"),
			@Result(column = "uid", property = "uid"),
			@Result(column = "content", property = "content"),
			@Result(column = "source", property = "source"),
			@Result(column = "pubtime", property = "pubtime"),
			@Result(column = "publish_time", property = "publish_time"),
			@Result(column = "md5", property = "md5"),
			@Result(column = "query", property = "query"),
			@Result(column = "read_count", property = "read_count"),
			@Result(column = "state", property = "state"),
			@Result(column = "category_id", property = "category", one = @One(select = "com.cp.weixin.blog.dao.CategoryMapper.getById")),
			@Result(column = "tags", property = "tags")
	})
	Record getRecordByUid(String uid);


	@Select("SELECT * FROM record WHERE md5 = #{md5}")
	@Results({
			@Result(id = true, column = "id", property = "id"),
			@Result(column = "md5", property = "md5")
	})
	Record getRecordByUrlMd5(String md5);

	@Select("SELECT * FROM record WHERE title = #{title}")
	@ResultMap(value = "allColumns")
	Record getRecordByTitle(String title);

	@Select("<script>SELECT id,uid,title,read_count FROM record WHERE state = #{state} <if test='categoryId!=0'> AND category_id = #{categoryId}</if> <if test='tagId!=0'> AND FIND_IN_SET(#{tagId}, tags) </if> ORDER BY id DESC</script>")
	@ResultMap(value = "allColumns")
	List<Record> getRecordByPageAndStateAndCategoryId( @Param("state") Integer state, @Param("categoryId") Integer categoryId, @Param("tagId") Integer tagId);

	@Update("UPDATE record SET title = #{title}, source = #{source}, content = #{content} WHERE id = #{id}")
	int updateRecord(@Param("title") String title, @Param("source") String source, @Param("content") String content, @Param("id") Integer id);

	@Update("UPDATE record SET title = #{title}, source = #{source}, content = #{content}, state = 1, publish_time = #{publish_time}, category_id = #{categoryId}, tags = #{tags} WHERE id = #{id}")
//	int updateAndPublishRecord(Record record);
	int updateAndPublishRecord(@Param("title") String title, @Param("source") String source, @Param("content") String content, @Param("publish_time") Date publish_time, @Param("categoryId") Integer categoryId, @Param("id") Integer id, @Param("tags") String tags);

	@Update("UPDATE record SET read_count = read_count + 1 WHERE uid = #{uid}")
	int readCountPlus(String uid);

	@Select("SELECT tags FROM record WHERE state = 1 AND FIND_IN_SET(#{tagId}, tags) ORDER BY id DESC")
	@ResultMap(value = "allColumns")
	List<Record> getByTagId(Integer tagId);
}
